package com.example.hanfu.login;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.hanfu.R;
import com.example.hanfu.ViewActivity;

public class LoginActivity extends AppCompatActivity {
    private static final String TAG = "LoginActivity";
    LoginDBManager manager = new LoginDBManager(LoginActivity.this);
    ;
    EditText id;
    EditText pwd;
    LoginState loginState=new LoginState();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
         id=findViewById(R.id.loginid);
         pwd=findViewById(R.id.loginpwd);
        Button login=findViewById(R.id.login);
        //SharedPreferences sharedPreferences=getSharedPreferences("login",LoginActivity.MODE_PRIVATE);
        //String username=sharedPreferences.getString("username","");
        //String pwd=sharedPreferences.getString("pwd","");
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断账号是否存在，不存在，返回提示
                String number=id.getText().toString();
                String password=pwd.getText().toString();
                if(id.getText().toString().length()==0||password.length()==0){
                    new AlertDialog.Builder(LoginActivity.this)
                            .setTitle("提示")
                            .setMessage("请输入完整登录信息")
                            .setPositiveButton("确定", null)
                            .show();
                }
                Login item=manager.findById(number);
                if(item!=null){
                    //登陆成功，到主页
                    loginState.setLogin_state(1);
                    loginState.setUserName(number);

                    Log.i(TAG,"当前登录状态："+loginState.getLogin_state());
                    Intent open=new Intent(LoginActivity.this, ViewActivity.class);
                    startActivity(open);
                }else{
                    //提示用户注册
                    new AlertDialog.Builder(LoginActivity.this)
                            .setTitle("提示")
                            .setMessage("此账号不存在，请先注册")
                            .setPositiveButton("确定", null)
                            .show();
                }
            }
        });

        Button register=findViewById(R.id.register);
        register.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            //跳转到注册页面
                                            Intent open=new Intent(LoginActivity.this, RegisterActivity.class);
                                            startActivity(open);
                                        }
                                    }

        );
    }


}